//
//  XMMeetAreaSelectView.m
//  XMJJ_zmy
//
//  Created by 丁浩 on 2025/7/17.
//

#import "XMMeetAreaSelectView.h"
#import "XMHAreaModel.h"

@interface XMMeetAreaSelectView ()<UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate>

@property (nonatomic, strong) UITableView *provinceTbl;
@property (nonatomic, strong) UITableView *cityTbl;
@property (nonatomic, strong) UITableView *streetTbl;

@property (nonatomic, strong) NSArray *provinceModelArr;
@property (nonatomic, strong) NSArray *cityModelArr;
@property (nonatomic, strong) NSArray *streetModelArr;

@property (nonatomic, assign) NSInteger provinceSelectIndex;
@property (nonatomic, assign) NSInteger citySelectIndex;
@property (nonatomic, assign) NSInteger streetSelectIndex;

@end

@implementation XMMeetAreaSelectView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    self.backgroundColor = kColorHEXA(0x464646, 0.46);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMiss:)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(400.0))];
    contentView.backgroundColor = kColorHEX(0xffffff);
    [contentView setPartCornerWithRectCorner:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadius:KScaleWidth(12.0)];
    [self addSubview:contentView];
    _provinceSelectIndex = -1;
    
    [contentView addSubview:self.provinceTbl];
    [contentView addSubview:self.cityTbl];
    [contentView addSubview:self.streetTbl];
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KBoldFontSize(16.0) frame:CGRectMake(self.width - KScaleWidth(10.0) - KScaleWidth(120.0), contentView.height - KScaleWidth(60.0) + (KScaleWidth(60.0) - KScaleWidth(40.0))/2, KScaleWidth(120.0), KScaleWidth(40.0)) title:@"确定" titleColor:kColorHEX(0x333333)];
    [confirmBtn setGradientLayerWithLocations:@[@(0), @(1.0)] colors:@[(__bridge id)kColorHEX(0xFFD800).CGColor, (__bridge id)kColorHEX(0xFFE44E).CGColor] startPoint:CGPointMake(0.5, 0.37) endPoint:CGPointMake(0.03, 0.37)];
    [confirmBtn setCornerRadius:confirmBtn.height/2];
    [confirmBtn addTarget:self action:@selector(confirmAction:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:confirmBtn];
    contentView.height = 1;
    self.alpha = 0;
}

- (void)setContentWithAreaModelArr:(NSArray *)areaModelArr {
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.provinceTbl.superview.height = KScaleWidth(400.0);
        weakSelf.alpha = 1;
    }];
    _provinceModelArr = areaModelArr;
    [_provinceTbl reloadData];
}

- (void)confirmAction:(UIButton *)button {
    if (_provinceSelectIndex >= 0 && _citySelectIndex >= 0 && _streetSelectIndex >= 0) {
        XMHAreaModel *provinceModel = _provinceModelArr[_provinceSelectIndex];
        XMHAreaModel *cityModel = provinceModel.children[_citySelectIndex];
        XMHAreaModel *streetModel = cityModel.children[_streetSelectIndex];
        self.areaSelectBlock ? self.areaSelectBlock([NSString stringWithFormat:@"%@-%@-%@", provinceModel.name, cityModel.name, streetModel.name]) : nil;
        [self disMiss:nil];
    }
    else {
        [MBProgressHUD showError:@"请选择所选区域"];
    }
}

- (void)disMiss:(UITapGestureRecognizer *)tap {
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.provinceTbl.superview.height = 1;
        weakSelf.alpha = 0;
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
}

#pragma mark - Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == _provinceTbl) {
        return self.provinceModelArr.count;
    }
    else if (tableView == _cityTbl) {
        return self.cityModelArr.count;
    }
    else {
        return self.streetModelArr.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return KScaleWidth(44.0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellName = @"XMMeetAreaSelectCellName";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    if (tableView == _provinceTbl) {
        XMHAreaModel *areaModel = _provinceModelArr[indexPath.row];
        cell.textLabel.text = areaModel.name;
        cell.backgroundColor = kColorHEX(0xF7F7F7);
        cell.textLabel.textColor = kColorHEX(0x999999);
        if (_provinceSelectIndex == indexPath.row) {
            cell.backgroundColor = kColorHEX(0xFFFFFF);
            cell.textLabel.textColor = kColorHEX(0x333333);
        }
    }
    else if (tableView == _cityTbl) {
        cell.backgroundColor = kColorHEX(0xFFFFFF);
        XMHAreaModel *areaModel = _cityModelArr[indexPath.row];
        cell.textLabel.text = areaModel.name;
        if (_citySelectIndex == indexPath.row) {
            cell.textLabel.font = KFontSize(17.0);
            cell.textLabel.textColor = kColorHEX(0x333333);
        }
        else {
            cell.textLabel.font = KFontSize(14.0);
            cell.textLabel.textColor = kColorHEX(0x818181);
        }
    }
    else {
        cell.backgroundColor = kColorHEX(0xFFFFFF);
        XMHAreaModel *areaModel = _streetModelArr[indexPath.row];
        cell.textLabel.text = areaModel.name;
        cell.textLabel.text = areaModel.name;
        if (_streetSelectIndex == indexPath.row) {
            cell.textLabel.font = KFontSize(17.0);
            cell.textLabel.textColor = kColorHEX(0x333333);
        }
        else {
            cell.textLabel.font = KFontSize(14.0);
            cell.textLabel.textColor = kColorHEX(0x818181);
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (tableView == _provinceTbl) {
        XMHAreaModel *areaModel = _provinceModelArr[indexPath.row];
        _cityModelArr = areaModel.children;
        _provinceSelectIndex = indexPath.row;
        _citySelectIndex = -1;
        _streetSelectIndex = -1;
        _streetModelArr = @[];
        [tableView reloadData];
        [_cityTbl reloadData];
        [_streetTbl reloadData];
    }
    else if (tableView == _cityTbl) {
        XMHAreaModel *areaModel = _cityModelArr[indexPath.row];
        _citySelectIndex = indexPath.row;
        _streetModelArr = areaModel.children;
        _streetSelectIndex = -1;
        [tableView reloadData];
        [_streetTbl reloadData];
    }
    else {
        _streetSelectIndex = indexPath.row;
        [tableView reloadData];
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if (touch.view == self) {
        return YES;
    }
    return NO;
}

#pragma mark -Init
- (UITableView *)provinceTbl {
    if (!_provinceTbl) {
        _provinceTbl = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScaleWidth(88.0), KScaleWidth(400.0) - KScaleWidth(60.0))];
        _provinceTbl.delegate = self;
        _provinceTbl.dataSource = self;
        _provinceTbl.separatorStyle = UITableViewCellSeparatorStyleNone;
        _provinceTbl.showsVerticalScrollIndicator = NO;
    }
    return _provinceTbl;
}

- (UITableView *)cityTbl {
    if (!_cityTbl) {
        _cityTbl = [[UITableView alloc] initWithFrame:CGRectMake(self.provinceTbl.maxX, 0, (self.width - KScaleWidth(88.0))/2, _provinceTbl.height)];
        _cityTbl.delegate = self;
        _cityTbl.dataSource = self;
        _cityTbl.separatorStyle = UITableViewCellSeparatorStyleNone;
        _cityTbl.showsVerticalScrollIndicator = NO;
    }
    return _cityTbl;
}

- (UITableView *)streetTbl {
    if (!_streetTbl) {
        _streetTbl = [[UITableView alloc] initWithFrame:CGRectMake(self.cityTbl.maxX, 0, self.cityTbl.width, self.cityTbl.height)];
        _streetTbl.delegate = self;
        _streetTbl.dataSource = self;
        _streetTbl.separatorStyle = UITableViewCellSeparatorStyleNone;
        _streetTbl.showsVerticalScrollIndicator = NO;
    }
    return _streetTbl;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
